package com.madinatek.designproject.client;

import com.madinatek.designproject.server.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class UploadingPanel extends JFrame implements ActionListener {
	private JList algorithmList = null;
	private JButton addButton = null;
	private JButton removeButton = null;
	private JButton uploadButton = null;
	private JLabel statusLabel = null;
	private JLabel listLabel = null;
	private JButton upload = null;
	//JButton videoStreaming = null;
	
	public UploadingPanel() {
		super("FPGA-based Firewire Camera Pre-Processor");
		setLayout(new BorderLayout());
		algorithmList = new JList();
		algorithmList.setSize(200, 200);
		addButton = new JButton("Add Algorithm Pkg");
		removeButton = new JButton("Remove Algorithm Pkg");
		uploadButton = new JButton("Upload");
		uploadButton.addActionListener(this);
		
		//videoStreaming = new JButton("Video Streaming");
		
		statusLabel = new JLabel();
		statusLabel.setText("Program started.");
		listLabel = new JLabel();
		listLabel.setText("List of algorithms");
		addAlgorithm();
		add(createMenu(), BorderLayout.NORTH);
		add(algorithmList, BorderLayout.CENTER);
		
		JPanel panelButton = new JPanel();
		panelButton.setLayout(new BorderLayout());		
		panelButton.add(uploadButton, BorderLayout.EAST);
		panelButton.add(statusLabel, BorderLayout.WEST);
		//videoStreaming.setSize(50, videoStreaming.getHeight());
		//panelButton.add(videoStreaming, BorderLayout.CENTER);
		
		add(panelButton, BorderLayout.SOUTH);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setSize(400,200);
		show();
	}
	
	JMenuItem aboutThisSoftwareMenu;

	JMenuItem exitMenu;
	JMenuItem videoStreaming;

	JMenuBar createMenu() {
		JMenuBar menuBar = new JMenuBar();

		// File Menu
		JMenu fileMenu = new JMenu("File");
		videoStreaming = new JMenuItem("Video Streaming");
		videoStreaming.addActionListener(this);
		fileMenu.add(videoStreaming);
		
		exitMenu = new JMenuItem("Exit");
		exitMenu.addActionListener(this);
		fileMenu.add(exitMenu);
		menuBar.add(fileMenu);

		// Help Menu
		JMenu helpMenu = new JMenu("Help");
		aboutThisSoftwareMenu = new JMenuItem("About This Software");
		helpMenu.add(aboutThisSoftwareMenu);
		aboutThisSoftwareMenu.addActionListener(this);
		menuBar.add(helpMenu);

		return menuBar;
	}
	
	void addAlgorithm() {
		algorithmList.setListData(AlgorithmPackage.search());		
	}
	
	Main m = new Main();
	
	public void actionPerformed(ActionEvent ae) {
		JComponent c = (JComponent) ae.getSource();
		if(c == aboutThisSoftwareMenu) {
			JOptionPane.showMessageDialog(this, "This software was developed as a part of project\n to demonstrate the power of FPGA to do real time\nvideo processing. This application is used " + 
				"to \nconfigure the TM4 board dynamically with new \nvideo processing algorithms.\n\nDesign Project 2006-2007.\nCopyright 2006-2007 Khaled, Youssef, Mike");
		}
		else if(c == videoStreaming) {
			m.start();
		}
		else if(c == exitMenu) {
			System.exit(0);
		} 
		else if(c == uploadButton) {
			Object selectedItem = algorithmList.getSelectedValue();
			if(selectedItem != null) {
				try {
					AlgorithmPackage selectedPkg = (AlgorithmPackage) selectedItem;
					//FileTransfer.SendFile("ugsparc204.eecg.toronto.edu", selectedPkg.FullFileName, "/nfs/ugsparcs/a-c/a-c/abdulka/dp");
					Socket socket = new Socket("ugsparc204.eecg.toronto.edu", 54035);
										
					ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
					
					TM4Packet packetToClient = new TM4Packet();
					packetToClient.FileName = selectedPkg.FileName;
					packetToClient.Type = TM4Packet.CLIENT_REQUEST;

					toServer.writeObject(packetToClient);
					
					ObjectInputStream fromServer = new ObjectInputStream(socket.getInputStream());
					TM4Packet packetFromServer = (TM4Packet) fromServer.readObject();										
					
					toServer.close();
					fromServer.close();
					socket.close();
					
					if(packetFromServer.Type == TM4Packet.SERVER_ACK) {
						JOptionPane.showMessageDialog(this, "Device successfully reconfigured with \"" + selectedItem + "\" algorithm.");
						statusLabel.setText("TM4 Programmed. [" + selectedItem + "]");
					}
				} 
				catch(ConnectException connectionEx) {
					JOptionPane.showMessageDialog(this, "Can not connect to TM4 Programming Host");
				}
				catch(Exception ex) 
				{
					//ex.printStackTrace();
					JOptionPane.showMessageDialog(this, "Unknown error occured.\n" + ex.getMessage());
				}
			}
		}
	}
}
